<template>
  <div class="review-container">
      <!-- 查询表单 -->
      <!-- <el-card class="search-card">
          <el-form :inline="true" :model="searchForm">
              <el-form-item label="发布者" prop="userName">
                  <el-input v-model="searchForm.userName" placeholder="用户名" clearable />
              </el-form-item>
              <el-form-item label="内容" prop="content">
                  <el-input v-model="searchForm.content" placeholder="帖子内容" clearable />
              </el-form-item>
              <el-form-item label="状态" prop="status" style="min-width: 200px">
                  <el-select v-model="searchForm.status" placeholder="审核状态" clearable style="width: 100%">
                      <el-option label="待审" :value="0" />
                      <el-option label="通过" :value="1" />
                      <el-option label="拒绝" :value="2" />
                  </el-select>
              </el-form-item>
              <el-form-item label="发布时间">
                  <el-date-picker v-model="searchForm.dateRange" type="daterange" range-separator="至"
                      start-placeholder="开始日期" end-placeholder="结束日期" value-format="YYYY-MM-DD" />
              </el-form-item>
              <el-form-item>
                  <el-button type="primary" @click="loadData">查询</el-button>
              </el-form-item>
          </el-form>
      </el-card> -->
      <el-card class="search-card">
        <el-form :model="searchForm" label-width="80px">
          <el-row :gutter="20">
            <el-col :xs="24" :sm="12" :md="8" :lg="6">
              <el-form-item label="用户名">
                <el-input v-model="searchForm.userName" placeholder="输入用户名" clearable />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6">
              <el-form-item label="昵称">
                <el-input v-model="searchForm.nickName" placeholder="输入用户昵称" clearable />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6">
              <el-form-item label="内容">
                <el-input v-model="searchForm.content" placeholder="输入帖子内容" clearable />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="24" :md="24" :lg="6">
              <el-form-item label="发布时间">
                <el-date-picker v-model="searchForm.timeRange" type="daterange" range-separator="至"
                  start-placeholder="开始日期" end-placeholder="结束日期" value-format="YYYY-MM-DD" />
              </el-form-item>
            </el-col>
          </el-row>
          <div class="search-actions">
            <el-button type="primary" @click="handleSearch">搜索</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </div>
        </el-form>
      </el-card>

      <!-- 内容区域 -->
      <el-scrollbar class="scroll-container">
          <div class="list-container">
              <el-card v-for="post in postList" :key="post.id" class="post-card">
                  <!-- 用户信息 -->
                  <div class="user-info">
                      <el-image :src="getAvatarUrl(post.user.avatar)" class="avatar"
                          :preview-src-list="[getAvatarUrl(post.user.avatar)]"></el-image>
                      <div class="user-details">
                          <div class="nickname">{{ post.user.nickName }}</div>
                          <div class="username">@{{ post.user.userName }}</div>
                          <div class="time">{{ post.createdTime }}</div>
                      </div>
                  </div>

                  <!-- 帖子内容 -->
                  <div class="post-content">{{ post.content }}</div>

                  <!-- 媒体内容 -->
                  <el-scrollbar v-if="post.urls && post.urls.length > 0" class="media-scroll">
                      <div v-if="post.urls && post.urls.length > 0" class="media-grid">
                          <div v-for="(url, index) in post.urls" :key="index" class="media-item">
                              <template v-if="isImage(url)">
                                  <el-image :src="getMediaUrl(url)" class="media-content"
                                      :preview-src-list="post.urls.filter(u => isImage(u)).map(u => getMediaUrl(u))"
                                      :initial-index="index" fit="contain" hide-on-click-modal>
                                      <template #error>
                                          <div class="media-error">
                                              <el-icon>
                                                  <Picture />
                                              </el-icon>
                                              <span>图片加载失败</span>
                                          </div>
                                      </template>
                                  </el-image>
                              </template>

                              <template v-else-if="isVideo(url)">
                                  <video controls class="media-video" :src="getMediaUrl(url)">
                                      <source :src="getMediaUrl(url)" type="video/mp4">
                                  </video>
                              </template>
                          </div>
                      </div>
                  </el-scrollbar>

              </el-card>
          </div>
      </el-scrollbar>

      <!-- 分页 -->
      <div class="pagination-container">
          <el-pagination v-model:current-page="pagination.current" v-model:page-size="pagination.size"
              :total="pagination.total" :page-sizes="[10, 20, 50]" layout="total, sizes, prev, pager, next" background
              @size-change="loadData" @current-change="loadData" />
      </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getPosts} from '../api/post'

const BASE_URL = 'http://localhost:8081'



// 响应式数据
const postList = ref([])
const searchForm = reactive({
  userName: '',
  nickName: '',
  content: '',
  timeRange: []
})
const pagination = reactive({
  current: 1,
  size: 10,
  total: 0
})

// URL处理方法
// 修改URL处理方法
const getMediaUrl = (filename) => {
  return `${BASE_URL}/video/${filename.replace(/^\//, '')}`
}

const getAvatarUrl = (url) => `${BASE_URL}/avatars/${url}`

const isImage = (url) => /\.(jpg|jpeg|png|gif)$/i.test(url)
const isVideo = (url) => /\.(mp4|webm|ogg)$/i.test(url)

// 加载数据
const loadData = async () => {
  try {
    const offset = (pagination.current - 1) * pagination.size
    const params = {
      offset,
      size: pagination.size,
      userName: searchForm.userName || undefined,
      nickName: searchForm.nickName || undefined,
      content: searchForm.content || undefined,
      startTime: searchForm.timeRange?.[0] || undefined,
      endTime: searchForm.timeRange?.[1] || undefined
    }

      const res = await getPosts(params)
      postList.value = res.data
      pagination.total = res.total
  } catch (error) {
      ElMessage.error('数据加载失败' + error)
  }
}


// 事件处理
const handleSearch = () => {
  pagination.current = 1
  loadData()
}

const resetSearch = () => {
  searchForm.value = {
    userName: '',
    nickName: '',
    content: '',
    timeRange: []
  }
  handleSearch()
}


// 初始化加载
onMounted(loadData)
</script>

<style scoped>
.review-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.search-card {
  margin-bottom: 10px;
  flex-shrink: 0;
}

.scroll-container {
  flex: 1;
  overflow: hidden;
}

.list-container {
  padding: 10px;
}

.post-card {
  margin-bottom: 20px;
}

.user-info {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  margin-right: 15px;
}

.user-details {
  flex: 1;
}

.nickname {
  font-weight: bold;
  margin-bottom: 3px;
}

.username {
  color: #666;
  font-size: 0.9em;
}

.time {
  color: #999;
  font-size: 0.8em;
}

.post-content {
  margin: 15px 0;
  white-space: pre-wrap;
}

/* 新增媒体网格样式 */
.media-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 10px;
  margin: 15px 0;
}

.media-item {
  position: relative;
  aspect-ratio: 1;
  background-color: #f5f5f5;
  border-radius: 4px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.media-content {
  width: 100%;
  height: 100%;
  object-fit: contain;
  transition: transform 0.3s ease;
}

.media-video {
  max-height: 400px;
  max-width: 100%;
  border-radius: 4px;
}

.media-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #999;
  font-size: 12px;
}

/* 调整操作按钮位置 */
.action-buttons {
  margin-top: 15px;
  display: flex;
  gap: 10px;
  align-items: center;
  border-top: 1px solid #eee;
  padding-top: 15px;
}

.status-tag {
  margin-left: auto;
}

.pagination-container {
  padding: 20px;
  background: white;
  box-shadow: 0 -1px 3px rgba(0, 0, 0, 0.1);
  z-index: 1;
}
</style>